package com.starbaba.stat.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.base.common.database.BaseDao;
import com.base.common.exception.AppException;
import com.base.common.util.StringUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.stat.service.StatMallItemService;
import com.starbaba.stat.utils.Constant;
import com.tuanzi.server.loan_base.model.db.stat.Stat_Mall_Item_Project;

@SuppressWarnings("rawtypes")
@Component
public class StatMallItemServiceImpl implements StatMallItemService {

	private BaseDao statdao = new BaseDao(Constant.LOAN_STAT_DB_POOL);// Constant.LOAN_STAT_DB_POOL

	public List queryList(Integer start, Integer length, PageResponse pageResponse, Map<String, Object> paramsMap) throws Exception {
		String startTime = (String) paramsMap.get("startTime");
		String endTime = (String) paramsMap.get("endTime");
		String itemId = (String) paramsMap.get("itemId");
		String itemName = (String) paramsMap.get("itemName");
		String page = (String) paramsMap.get("page");
		String module = (String) paramsMap.get("module");
		String position = (String) paramsMap.get("position");
		String platform = (String) paramsMap.get("platform");
		String prodId = (String) paramsMap.get("prodId");
		String channel = (String) paramsMap.get("channel");
		String userType = (String) paramsMap.get("userType");
		
		String sql = "SELECT i.time,i.platform,i.prod_id,i.channel,c.channel_name channelName,p.name prodName,i.user_type,i.page,i.module,i.position,i.item_id,i.item_name, "
				+ "sum(i.item_pv) item_pv,sum(i.item_uv) item_uv "
				+ "FROM stat.stat_mall_item_project i "
				+ "LEFT OUTER JOIN loan.comm_product_channel c on i.channel = c.channel_code "
				+ "LEFT OUTER JOIN loan.comm_product_prdid p ON i.prod_id = p.prdid "
				+ "WHERE 1=1 ";
		
		String selectSql = "";
		if (!StringUtils.isEmpty(startTime)) {
            selectSql += " AND i.time >= '" + startTime  + "'";
        }
		if (!StringUtils.isEmpty(endTime)) {
			selectSql += " AND i.time <= '" +endTime + "'";
		}
		if (!StringUtils.isEmpty(itemId)) {
            selectSql += " AND i.item_id = " + itemId;
        }
		if (!StringUtils.isEmpty(itemName)) {
			selectSql += " AND i.item_name like '%" +itemName + "%'";
		}
		if ("ios".equals(platform) || "android".equals(platform) || "-".equals(platform)) {
			selectSql += " AND i.platform = '" +platform +"' ";
		}
		if (!StringUtils.isEmpty(prodId)) {
			selectSql += " AND i.prod_id = " +prodId;
		}
		if (!StringUtils.isEmpty(channel)) {
			selectSql += " AND i.channel = " +channel;
		}
		if (!StringUtils.isEmpty(userType)) {
			selectSql += " AND i.user_type = '" +userType + "'";
		}
		
		String groupSql = " GROUP BY i.time, ";
		if (!StringUtils.isEmpty(itemId) || !StringUtils.isEmpty(itemName)) {
			groupSql += " i.item_id, ";
		}
		if (!StringUtils.isEmpty(page)) {
			groupSql += " i.page, ";
		}
		if (!StringUtils.isEmpty(module)) {
			groupSql += " i.module, ";
		}
		if (!StringUtils.isEmpty(position)) {
			groupSql += " i.position, ";
		}
		if (!StringUtils.isEmpty(platform)) {
			groupSql += " i.platform, ";
		}
		if (!StringUtils.isEmpty(prodId)) {
			groupSql += " i.prod_id, ";
		}
		if (!StringUtils.isEmpty(channel)) {
			groupSql += " i.channel, ";
		}
		if (!StringUtils.isEmpty(userType)) {
			groupSql += " i.user_type, ";
		}
		groupSql = (String) groupSql.subSequence(0, groupSql.length()-2);

		sql += selectSql + groupSql;
		String listSql = sql + " order by i.time desc ";
		
		if(start != null && length != null){
			listSql = listSql + " limit " + start + " , " + length;
		}

		String countSql = "select count(1) FROM (" + sql +") a";
		
		List<Stat_Mall_Item_Project> list = null;
		try {
			int count = statdao.getInt(countSql);
			list = statdao.list(listSql, Stat_Mall_Item_Project.class, new Object[] {});
			
			for(Stat_Mall_Item_Project model:list){
				if (StringUtils.isEmpty(itemId) && StringUtils.isEmpty(itemName)) {
					model.setItem_id(0L);
					model.setItem_name("全部");
				}
				if (StringUtils.isEmpty(page)) {
					model.setPage("全部");
				}
				if (StringUtils.isEmpty(module)) {
					model.setModule("全部");
				}
				if (StringUtils.isEmpty(position)) {
					model.setPosition("全部");
				}
				if (StringUtils.isEmpty(platform)) {
					model.setPlatform("全部");
				}
				if (StringUtils.isEmpty(prodId)) {
					model.setProdName("全部");
				}
				if (StringUtils.isEmpty(channel)) {
					model.setChannelName("全部");
				}
				if (StringUtils.isEmpty(userType)) {
					model.setUser_type("全部");
				}
			}
			
			pageResponse.setITotalRecords(count);
			pageResponse.setITotalDisplayRecords(count);
		} catch (AppException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
}
